﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MethodWorx.CMS.TemplateLanguage.Design;
using MethodWorx.CMS.TemplateLanguage.Properties;

namespace MethodWorx.CMS.TemplateLanguage.Parsers.Html
{
    public class Head : IParser, IDesignable
    {
        [Property]
        public string Title { get; set; }
        [Property]
        public bool IncludeCss { get; set; }
        [Property]
        public bool IncludeScript { get; set; }

        public object Parse(Web.CmsRequestContext context, IEnumerable<object> stack, Dictionary<string, object> parameters)
        {
            return GetContent();
        }

        private HtmlResult GetContent()
        {
            var result = new HtmlResult();

            result.PreContent.Append("<head>");
            if (string.IsNullOrEmpty(this.Title))
                result.PreContent.AppendFormat("<title>{0}</title>", this.Title);

            if (this.IncludeCss)
                result.PreContent.AppendFormat("<link type=\"text/css\" rel=\"stylesheet\" href=\"?__css=true\"/>");
            if (this.IncludeScript)
                result.PreContent.AppendFormat("<script type=\"text/javascript\" src=\"?__script=true\"></script>");

            result.PostContent.Append("</head>");

            return result;
        }

        public string Name
        {
            get { return "Head";  }
        }

        public string Namespace
        {
            get { return "Html"; }
        }
    
        public void  Design(IDesigner designer)
        {
            var content = this.GetContent();
 	        //
            //  dont worry about this yet.. this is
            //  for the component designer 
            designer.AddControl("html:head")
                .SetControlType(DesignerControlType.Component)
                .AddContentBeforeChildren(content.PreContent.ToString())
                .AddContentAfterChildren(content.PostContent.ToString());
        }
    }
}
